package com.openbravo.pos.panels.navigator;

import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;

import e.odbo.data.dao.I_DataLogic;
import cn.ekuma.data.dao.user.BrowsableEditableDataModel;

import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JListNavigator;
import com.openbravo.data.gui.ListCellRendererBasic;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecordAdapter;
import com.openbravo.pos.panels.event.UserChangedListener;

public class JUserNavigator extends JListNavigator {
	I_DataLogic dataLogic;
	public JUserNavigator(I_DataLogic dataLogic) {
		super(new BrowsableEditableDataModel(dataLogic,UserInfo.class,new EditorRecordAdapter<UserInfo>(),new DirtyManager()));
	    setCellRenderer(getListCellRenderer());
	    this.dataLogic=dataLogic;
	}

	public void loadData() throws BasicException{
		m_bd.loadData(dataLogic.list(UserInfo.class));
    }

	@Override
	public void valueChanged(ListSelectionEvent evt) {
		super.valueChanged(evt);
		for(UserChangedListener l:listenerList.getListeners(UserChangedListener.class)){
			l.valueSelectChanged( (UserInfo) m_bd.getCurrentElement());
		}
	}
	
	public void addUserChangedListener(UserChangedListener l){
		listenerList.add(UserChangedListener.class, l);
	}
	
	public void removeUserChangedListener(UserChangedListener l){
		listenerList.remove(UserChangedListener.class, l);
	}
	
	 public ListCellRenderer getListCellRenderer() {
	        return new ListCellRendererBasic(new IRenderString<UserInfo>(){
				@Override
				public String getRenderString(UserInfo value) {	
					return value.getName();
				}
	            
	        });
	    }
}
